package com.wgl.va.manager;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.wgl.va.recorder.Recorder;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTextField voiceCmdPath;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panelOper = new JPanel();
		contentPane.add(panelOper, BorderLayout.SOUTH);

		JButton recordBtn = new JButton("\u5F55\u97F3");
		recordBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// recordBtn.setEnabled(false);
				Recorder recorder = new Recorder();
				recorder.capture();
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} finally {
					recorder.stop();
					recorder.save();
					// recordBtn.setEnabled(true);

				}
			}
		});
		panelOper.add(recordBtn);

		JButton exitBtn = new JButton("\u9000\u51FA");
		panelOper.add(exitBtn);

		JPanel panelText = new JPanel();
		contentPane.add(panelText, BorderLayout.NORTH);

		JButton pathBtn = new JButton("\u8DEF\u5F84");
		pathBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFrame f = new JFrame();
				JFileChooser jfc = new JFileChooser();
				jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				if (jfc.showOpenDialog(f) == JFileChooser.APPROVE_OPTION) {
					String path = jfc.getSelectedFile().getAbsolutePath();
					voiceCmdPath.setText(path);
				}
			}
		});
		panelText.add(pathBtn);

		voiceCmdPath = new JTextField();
		voiceCmdPath.setEditable(false);
		panelText.add(voiceCmdPath);
		voiceCmdPath.setColumns(10);
	}

}
